# import schedule
import time
from playwright.sync_api import sync_playwright
import subprocess


command = r'"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --user-data-dir="C:\Users\wangy\AppData\Local\Google\Chrome\User Data"'
subprocess.Popen(command, shell=True)
def get_youtube_cookies():
    with sync_playwright() as p:
        # 确保 Chrome 已以 --remote-debugging-port=9223 启动
        browser = p.chromium.connect_over_cdp("http://localhost:9222")
        page = browser.new_page()
        page.goto("https://www.youtube.com")
        page.reload()
        cookies = page.context.cookies()
        with open('cookies.txt', 'w') as f:
            for cookie in cookies:
                f.write(f"{cookie['name']}={cookie['value']}; ")
        browser.close()


get_youtube_cookies()

# 定时任务，每 20 分钟执行一次
# schedule.every(20).minutes.do(get_youtube_cookies)

# while True:
#     schedule.run_pending()
#     time.sleep(1)
    